package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    EditText et_address;
    Button btn_new,btn_old;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_address = findViewById(R.id.et_address);
        btn_new = findViewById(R.id.btn_new);
        btn_old = findViewById(R.id.btn_old);


        btn_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initParse(true);
            }
        });

        btn_old.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initParse(false);
            }
        });

    }

    /**
     * 调用解析
     * @param isNew
     */
    private void initParse(boolean isNew) {
        URL url = null;
        try {
            url = new URL(et_address.getText().toString().trim());
            PlaylistDownloader2 downloader = new PlaylistDownloader2(url,isNew);
            try {
                downloader.parse(new PlaylistDownloader2.ParseListener() {
                    @Override
                    public void parseResult(ArrayList<M3u8Source> list) {
                        Toast.makeText(MainActivity.this, "看logcat打印", Toast.LENGTH_SHORT).show();
                        for (M3u8Source m3u8Source : list) {
                            Log.d("lcf*******", "分辨率："+m3u8Source.getVideoResolution()+" 源地址："+m3u8Source.getVideoRealUrl());
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


}
